<?php

namespace App\Http\Controllers\Wap;

use App\Model\Comment;
use App\Model\Info;
use App\Model\InfoType;
use Illuminate\Http\Request;
use App\Model\Member;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{

    public function __construct(Request $request){
        $this->request = $request;
        $this->openid = session('wechat_user')['id'];
        if (!empty($this->openid)) {
            $this->memberId = Member::where('openid',$this->openid)->pluck('id');
        }
    }

    /**
     * 资讯列表
     * czs
     */
    public function index(){
        $infoType = $this->request->input('id');
        $res = Info::infoList($infoType,6)->getData();
        $menu = InfoType::typePull()->getData();
        return view('wap.article.index')
            ->with('res',$res)
            ->with('menu',$menu);
    }
    public function indexAjax() {
        $infoType = $this->request->input('id');
        $res = Info::infoList($infoType,6)->getData();
        $html = view('wap.article.index-ajax')->with('res',$res)->render();
        $data = [
            'html'=>$html,
            'more'=>$res->hasMorePages()?1:0
        ];
        return $data;
    }

    /**
     * 资讯详情页
     * czs
     */
    public function articleDetail($id){
        if ($this->request->isMethod('post')) {
            $content = $this->request->input('content');
            Comment::addComment($content,$id,$this->memberId,$site=2);
            return redirect('/wap/articleDetail/'.$id);
        } else {
            $res = Info::getOne($id);
            $comments = Comment::commentList($id,$site=2)->getData();
            return view('wap.article.articleDetail')
                ->with('res', $res)
                ->with('comments',$comments);
        }
    }
}
